package com.javasm.adminapi.module.product1.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.javasm.adminapi.module.product1.entity.GgGoods;
import com.javasm.adminapi.module.product1.vo.SearchVo;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 商品管理-商品管理(GgGoods)表数据库访问层
 *
 * @author makejava
 * @since 2025-10-15 20:31:10
 */
public interface GgGoodsDao extends BaseMapper<GgGoods> {



    @Select("select gg.*,rsg.sid from \n" +
            "gg_supplier_goods rsg,gg_goods gg\n" +
            "where rsg.gid=gg.id\n" +
            "and sid=#{supplierId} \n")
    List<GgGoods> selectGoodsBySupplierId(Integer supplierId);


    @Select({
            "<script>",
            "select gg.*, gt.type_name",
            "from gg_goods gg, gg_goods_type gt",
            "where 1=1",  // 用 1=1 作为基础条件，避免 where 后面直接跟 and
            "and gg.gtype = gt.id",  // 原关联条件用 and 拼接
            // 商品名称模糊查询
            "<if test='pname != null and pname != \"\"'>",
            "and gg.gname like concat('%', #{pname}, '%')",
            "</if>",
            // 商品型号模糊查询
            "<if test='pmodel != null and pmodel != \"\"'>",
            "and gg.gmodel like concat('%', #{pmodel}, '%')",
            "</if>",
            // 按查询条件（类型/价格/库存）
            "<if test='queryconditon != null and queryconditon != \"\"'>",
            "<choose>",
            "<when test='queryconditon == \"byType\"'>",
            "and gt.type_name like concat('%', #{inputVal}, '%')",
            "</when>",
            "<when test='queryconditon == \"byPrice\"'>",
            "and gg.purchase_price like concat('%', #{inputVal}, '%')",
            "</when>",
            "<when test='queryconditon == \"byStore\"'>",
            "and gg.min_stock like concat('%', #{inputVal}, '%')",
            "</when>",
            "</choose>",
            "</if>",
            // 商品颜色
            "<if test='pcolor != null'>",
            "and gg.gcolor = #{pcolor}",
            "</if>",
            // 商品品牌
            "<if test='pbrand != null and pbrand != \"\"'>",
            "and gg.gbrand = #{pbrand}",
            "</if>",
            // 商品分类
            "<if test='ptype != null'>",
            "and gg.gtype = #{ptype}",
            "</if>",
            "</script>"
    })
   /* @Results(id="listAllWithTypeName",value = {
            @Result(property = "typeName",column = "type_name",
                    one = @One(select = "com.javasm.mybatisplusdemo.adminrole.dao.AdminRoleDao.selectById")),
            @Result(property = "roleId",column = "role_id"))
})*/
    List<GgGoods> selectAllWithTypeName(SearchVo vo);

}

